<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min(1).css">
    <link rel="stylesheet" href="../css/style.css">


</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统管理<small>菜单管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">


                <el-input placeholder="菜单名称" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="pagination.currentPage = 1; findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table
                    class="tree-table"
                    :data="dataList"
                    style="width: 100%;margin-bottom: 20px;"
                    row-key="id"
                    border
                    lazy
                    :load="load"
                    :tree-props="{children: 'children'}">
                <el-table-column
                        prop="name"
                        label="菜单名称"
                        sortable
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="linkUrl"
                        label="访问路径"
                        sortable
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="path"
                        label="路径级别">
                </el-table-column>

                <el-table-column
                        prop="parentMenuId"
                        label="父级菜单id">
                </el-table-column>
                <el-table-column
                        prop="level"
                        label="菜单级别">
                </el-table-column>

                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="访问路径" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>

                            <el-col :span="12">
                                <el-form-item label="路径级别" prop="path">
                                    <el-input v-model="formData.path"/>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <el-row>

                            <el-row>

                                <el-col :span="12">
                                    <el-form-item label="父菜单">
                                        <el-select v-model="formData.parentMenuId" placeholder="请选择">
                                            <el-option
                                                    v-for="firstMenuq in firstMenu"
                                                    :key="firstMenuq.id"
                                                    :label="firstMenuq.name"
                                                    :value="firstMenuq.id">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                            </el-row>

                        </el-row>

                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="图标" prop="icon">
                                    <el-input v-model="formData.icon"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>

                            <el-col :span="24">
                                <el-form-item label="注意事项" prop="description">
                                    <el-input v-model="formData.description"/>
                                </el-form-item>
                            </el-col>
                        </el-row>


                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑菜单" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="访问路径" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>

                            </el-col>
                            <el-col :span="12">
                            <el-form-item label="路径级别" prop="path">
                            <el-input v-model="formData.path"/>
                        </el-form-item></el-col>

                        </el-row>



                        <el-row>

                            <el-row>

                                <el-col :span="12">
                                    <el-form-item label="父菜单" prop="level">
                                        <el-select v-model="formData.parentMenuId" placeholder="请选择">
                                            <el-option
                                                    v-for="firstMenuq in firstMenu"
                                                    :key="firstMenuq.id"
                                                    :label="firstMenuq.name"
                                                    :value="firstMenuq.id">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="24">
                                    <el-form-item label="图标" prop="icon">
                                        <el-input v-model="formData.icon"/>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>

                                <el-col :span="24">
                                    <el-form-item label="注意事项" prop="description">
                                        <el-input v-model="formData.description"/>
                                    </el-form-item>
                                </el-col>
                            </el-row>

                        </el-row>
                        <el-row>

                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>

<!-- 引入样式 -->
<link rel="stylesheet" href="../plugins/elementui/lib/theme-chalk/index.css">
<!-- 引入组件库 -->
<script src="../plugins/elementui/lib/index.js"></script>
<script>

    var vue = new Vue({
        el: '#app',
        data: {
            firstMenu:[{id:"111",name:"aaa"},{id:"222",name:"bbb"}],
            menuIDList: [],// 菜单选择数据
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            dataList: [{}],//当前页要展示的分页列表数据
            formData: {parentMenuId:"111"},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            rules: {//校验规则
                name: [{required: true, message: '菜单名称为必填项', trigger: 'blur'}],
                path: [{required: true, message: '路径级别为必填项', trigger: 'blur'}],
                linkUrl: [{required: true, message: '访问路径为必填项', trigger: 'blur'}],
                level: [{required: true, message: '菜单级别为必填项', trigger: 'blur'}],


            },

            options: [],
            levels:[1,2,3]
            /*[{
                value: '选项1',
                label: '黄金糕'
            }, {
                value: '选项2',
                label: '双皮奶'
            }, {
                value: '选项3',
                label: '蚵仔煎'
            }, {
                value: '选项4',
                label: '龙须面'
            }, {
                value: '选项5',
                label: '北京烤鸭'
            }]*/
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            //一进入页面就要就行分页查询功能
            this.findPage();

            axios.get("/user/findSidebar.do").then((resp) => {
                this.menuList = resp.data.data;
            });

        },
        methods: {
            change(val) {
                let nodesObj = this.$refs['cascader'].getCheckedNodes();
                if (nodesObj[0].value == null) {
                    this.id = 999999;
                    console.log(this.id);
                } else {
                    this.id = nodesObj[0].value;
                    console.log(this.id);

                }


            },

            //权限不足提示
            showMessage(r) {
                if (r == 'Error: Request failed with status code 403') {    //权限不足    
                    this.$message.error('无访问权限');
                    return;
                } else {
                    this.$message.error('未知错误');
                    return;
                }
            },

            //编辑
            handleEdit() {


                //发送异步请求("访问的地址",前端页面传给后台的参数)then是响应可以得到后台的数据
                axios.post("/menu/edit.do" ,this.formData).then((resp) => {
                    //后台成功的响
                    if (resp.data.flag) {

                        this.$message({message: resp.data.message, type: "success"});
                    }
                    //后台失败的响应
                    else {
                        this.$message.error("此有子菜单"+resp.data.message+",为一级菜单，不可再有父级菜单");
                    }
                    //不论结果怎么样都会执行的方法
                }).finally(() => {
                    //关闭编辑的页面
                    this.dialogFormVisible4Edit = false;
                    //进行分页查询
                    this.findPage();
                });
            }
            ,
            //添加
            handleAdd() {
                axios.post("/menu/add.do",this.formData).then((res) => {
                    this.dialogFormVisible = false;
                    if (res.data.flag) {//执行成功
                        //添加成功后，重新调用分页查询方法，查询出最新的数据
                        this.findPage();
                        //弹出提示信息
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    } else {//执行失败
                        //弹出提示
                        this.$message.error(res.data.message);
                    }
                });
            }
            ,
            //分页查询
            findPage() {
                //前端页面需要提供参数,当前页,页面大小,查询条件
                let param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };


                //使用异步请求,后台响应回来的数据有总记录数和这一页的页面信息
                axios.post("/menu/pageQuery.do", param).then((resp) => {
                    this.pagination.total = resp.data.total;
                    this.dataList = resp.data.rows;
                });
            }
            ,
            // 重置表单
            resetForm() {
                this.formData = {};
                this.options = [];
                this.menuIDList = [];

            }
            ,
            // 弹出添加窗口
            handleCreate() {
                axios.post("/menu/findlevel1Menu.do")
                    .then((resp) => {
                        if (resp.data.flag) {
                            this.firstMenu = resp.data.data;

                        } else {
                            this.$message.error(resp.data.message);
                        }
                    });


                //清除表单二次验证的残留
                if (this.$refs.dataAddForm) {
                    this.$refs.dataAddForm.resetFields();
                }
                //添加表单开启
                this.dialogFormVisible = true;
                //调用重置表单
                this.resetForm();

            }
            ,
            // 弹出编辑窗口
            handleUpdate(row) {
                //清除表单二次验证的残留
                if (this.$refs.dataEditForm) {
                    this.$refs.dataEditForm.resetFields();
                }
                //1. 弹出编辑表单
                this.dialogFormVisible4Edit = true;
                //2. 显示表单数据(数据的回显)
                axios.post("/menu/findlevel1Menu.do")
                    .then((resp) => {
                        if (resp.data.flag) {
                            this.firstMenu = resp.data.data;
                            axios.get("/menu/findById.do?id=" + row.id).then((resp) => {
                                if (resp.data.flag) {
                                    this.formData = resp.data.data;

                                } else {
                                    this.message.error(resp.data.message);
                                }
                            });

                        } else {
                            this.$message.error(resp.data.message);
                        }
                    });


            }
            ,
            //切换页码
            handleCurrentChange(currentPage) {
                //设置最新的页码
                this.pagination.currentPage = currentPage;
                //重新调用findPage方法进行分页查询
                this.findPage();
            }
            ,
            // 删除
            handleDelete(row) {
                this.$confirm("你确定要删除" + row.name + "的全部信息吗？", "提示", {type: 'warnig'})
                    .then(() => {
                        axios.get("/menu/delete.do?id=" + row.id)
                            .then((res) => {
                                if (res.data.flag) {
                                    //执行成功
                                    //弹出成功提示信息
                                    this.$message({
                                        type: 'success',
                                        message: res.data.message
                                    });
                                    //重新进行分页查询
                                    this.findPage();
                                } else {
                                    this.$message.error(res.data.message);

                                }
                            }).catch((r) => {
                            this.showMessage(r);
                        });
                    })
                    .catch(() => {
                        this.$message({
                            type: 'info',
                            message: '操作已取消'
                        });
                    });


            }
        },
        watch: {
            menuIDList() {
                if (this.$refs.cascader) {
                    this.$refs.cascader.dropDownVisible = false
                }
            },
            formData() {
                if (this.$refs.cascader) {
                    this.$refs.cascader.dropDownVisible = false
                }
            }
        }


    })
</script>
</html>